<?php
/**
 * Created by PhpStorm.
 * User: soulmates
 * Date: 2017/8/15
 * Time: 上午8:06
 */
namespace app\restaurantadmin\controller;
use think\Controller;
class Food extends Controller
{
    public function food()
    {
        $uid=session('id');
        $shop= \think\Db::name('dining') ->where('admin_id',$uid)->find();
        $shopid=$shop['shop_id'];
        // var_dump($shopid);die;
        $foodtypes= \think\Db::name('type')->where('shop_id',$shopid)->select();
        // var_dump($foodtypes);die;
        $this->assign('foodtype',$foodtypes);


        //显示当前店铺的所有菜品分类
        $id=input('type_id');
        $type= \think\Db::name('type')->where('type_id',$id)->find();
        // var_dump($id);
        $this->assign('type',$type);


        //显示当前分类下的所有菜品
        $where=array();
        $where['shop_id']=$shopid;
        $where['type_id']=$id;
        // dump($where);
        $foodlist= \think\Db::name('food')->where($where)->order('food_id desc')->select();
        // var_dump($foodlist);die;
        $this->assign('foodlist',$foodlist);


        //根据foodid查找相关菜品信息
        $food_id=input('food_id');
        $food= \think\Db::name('food')->where('food_id',$food_id)->find();
        $this->assign('food',$food);

        return $this->fetch();
    }

    //添加菜品信息
    public function addfood(){

        $uid=session('id');
        $shop= \think\Db::name('dining')->where('admin_id',$uid) ->find();
        $shopid=$shop['shop_id'];

        if(request()->isPost()){
            $data=[
                'shop_id'=>$shopid,
                'foodname'=>input('foodname'),
                'type_id'=>input('type_id'),
                'price'=>input('price'),
                'state'=>'1',
                'isspecial'=>input('isspecial'),
                'recommand'=>input('recommand'),
                'specialprice'=>input('specialprice'),
                'startdate'=>input('startdate'),
                'enddate'=>input('enddate'),
                'fee'=>input('fee'),
                'intro'=>input('intro'),
                'addate'=>date("Y-m-d H:i:s"),
                ];

            //上传图片
            if($_FILES['pic']['tmp_name']){

                // 获取表单上传文件 例如上传了001.jpg
                $file = request()->file('pic');
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/admin/Uploads/foodpic');
                if($info){

                    $data['pic']=date('Ymd').'/'.$info->getFilename();
                    // echo $data['pic'];die;
                    // 输出 42a79759f284b767dfcb2a0197904287.jpg
                    // echo $info->getFilename();
                }else{
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }

            //添加数据前应该先验证
            $validate = \think\Loader::validate('Foodval');

            if($validate->check($data)){
                $db=\think\Db::name('food')->insert($data);
                if($db){
                    return $this->redirect('Food/food',['type_id'=>$data['type_id']]);
                }
                else{
                    return $this->error('添加失败','index');
                }
            }else{
                $this->error($validate->getError());
            }
            return;



        }
    }


    //添加分类
    public function addtype(){
        $uid=session('id');
        // echo("<script type='text/javascript'> alert($uid);</script>");
        $shop= \think\Db::name('dining')->where('admin_id',$uid)->find();
        $shopid=$shop['shop_id'];
        // var_dump($shopid);die;

        if(request()->isPost()){
            $data=[
                'type_name'=>input('type_name'),
                'shop_id'=>$shopid,
            ];
            // var_dump($data) ;die;
            //添加数据前应该先验证
            $validate = \think\Loader::validate('Foodtype');

            if($validate->check($data)){
                $db=\think\Db::name('type')->insert($data);
                if($db){
                    return $this->redirect('Food/food');
                }
                else{
                    echo("<script type='text/javascript'> alert('添加失败！');</script>");
                }
            }else{
                // echo $validate->getError();
                $this->error($validate->getError());
                // $error=$validate->getError();
                // echo("<script type='text/javascript'> alert($error);</script>");
            }

        }



    }

    public function typemenu(){

        $uid=session('id');
        $shop= \think\Db::name('dining')->where('admin_id',$uid)->find();
        $shopid=$shop['shop_id'];
        // var_dump($shopid);die;

        $foodtypes= \think\Db::name('type')->where('shop_id',$shopid)->select();
        // var_dump($foodtypes);die;
        $this->assign('foodtype',$foodtypes);
        return $this->fetch();
    }

    //删除菜品信息
    public function delfood(){

        $food_id=input('food_id');
        $type_id=input('type_id');
        // var_dump($type_id);die;
        $food= \think\Db::name('food')->where('food_id',$food_id)->find();
        $pic=$food['pic'];
        $file=APP.'public/static/index/Uploads/foodpic/'.$pic;
        // var_dump($file);die;

        if(db('food')->delete($food_id)){
            unlink($file);

            return $this->redirect('Food/food',['type_id'=>$type_id]);

        }else{
            return $this->error('删除失败！','foodlist');
        }
    }

    //停售
    public function stopsale(){

        $food_id=input('food_id');
        $type_id=input('type_id');


        if($db=\think\Db::name('food')->where('food_id',$food_id)->update(['state' =>'2','isspecial'=>'0'])){

            return $this->redirect('Food/food',['type_id'=>$type_id]);
        }else{
            return $this->error('设置失败！');
        }
        return;
    }


    //开售
    public function sale(){

        $food_id=input('food_id');
        $type_id=input('type_id');


        if($db=\think\Db::name('food')->where('food_id',$food_id)->update(['state' =>'1'])){

            return $this->redirect('Food/food',['type_id'=>$type_id]);
        }else{
            return $this->error('设置失败！');
        }
        return;
    }

    //设置主打菜
    public function norecommond(){

        $food_id=input('food_id');
        $type_id=input('type_id');


        if($db=\think\Db::name('food')->where('food_id',$food_id)->update(['recommand' =>'0'])){

            return $this->redirect('Food/food',['type_id'=>$type_id]);
        }else{
            return $this->error('设置失败！');
        }
        return;
    }

    //设置特色菜
    public function isrecommond(){

        $food_id=input('food_id');
        $type_id=input('type_id');


        if($db=\think\Db::name('food')->where('food_id',$food_id)->update(['recommand' =>'1'])){

            return $this->redirect('Food/food',['type_id'=>$type_id]);
        }else{
            return $this->error('设置失败！');
        }
        return;
    }



    //修改菜品信息
    public function edit_food(){
        $uid=session('id');
        $shop= \think\Db::name('dining')->where('admin_id',$uid)->find();
        $shopid=$shop['shop_id'];
        // var_dump($shopid);die;

        $foodtypes= \think\Db::name('type')->where('shop_id',$shopid)->select();
        // var_dump($foodtypes);die;
        $this->assign('foodtype',$foodtypes);




        $food_id=input('food_id');
        $food= \think\Db::name('food')->where('food_id',$food_id)->find();

        // $date=$food['startdate'];
        // $transdate=$date->format('d F,Y');

        $this->assign('food',$food);


        $type_id=input('type_id');
        //    echo $type_id;
        //    die();
        // var_dump($type_id);die;

        //获取修改后的数据
        if(request()->isPost()){
            $data=[

                'foodname'=>input('foodname'),
                'price'=>input('price'),
                'isspecial'=>input('isspecial'),
                'recommand'=>input('recommand'),
                'specialprice'=>input('specialprice'),
                'startdate'=>input('startdate'),
                'enddate'=>input('enddate'),
                'fee'=>input('fee'),
                'intro'=>input('intro'),
                'pic'=>input('pic'),
                'type_id'=>input('type_id'),

            ];




            //上传图片
            if($_FILES['pic']['tmp_name']){


                // 获取表单上传文件 例如上传了001.jpg
                $file = request()->file('pic');

                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/admin/Uploads/foodpic');
                if($info){


                    $data['pic']=date('Ymd').'/'.$info->getFilename();
                    // echo $data['pic'];die;
                    // 输出 42a79759f284b767dfcb2a0197904287.jpg
                    // echo $info->getFilename();
                }else{
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }

            // $type_id=input('type_id');
            if($db=\think\Db::name('food')->where('food_id',$food_id)->update($data)){

                // echo $type_id;
                // die();
                // dump($data['type_id']);die();
                // header("location:'javascript:history.back(-1);");
                // return $this->redirect('Food/food/',array('type_id'=>$type_id));
                return $this->redirect('Food/food',['type_id'=>$data['type_id']]);

            }else{
                return $this->error('修改失败！');
            }
            return;

        }

        return $this->fetch();
    }



}

